package com.resource.service.impl;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.resource.dto.OfficeProjectInfoDto;
import com.resource.query.ProjectQuery;
import com.resource.response.OfficeContractInfoResp;
import com.resource.service.ContractService;
import com.resource.service.OfficeProjectService;
import com.resource.service.OfficeServerService;
import com.resource.spring.ApiAssert;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * 泛微查询服务
 *
 * @Author: xianggaoxiong
 * @Date: 2020-09-24 18:48
 **/
@Slf4j
@Service
public class OfficeServerServiceImpl implements OfficeServerService {
	@Resource
	private OfficeProjectService projectService;
	@Resource
	private ContractService contractService;

	@Override
	public List<OfficeContractInfoResp> listContractInfo(String platformName, String platformCode) {
		List<OfficeContractInfoResp> resps = new ArrayList<>();
		ApiAssert.isTrue("请填写平台账号或平台名称", StrUtil.isEmpty(platformName) && StrUtil.isEmpty(platformCode));
		ProjectQuery projectQuery = new ProjectQuery();
		projectQuery.setManager_type("2");
		projectQuery.setField_3(platformCode);
		List<OfficeProjectInfoDto> projectList = projectService.getProjectList(projectQuery);
		if (CollUtil.isEmpty(projectList)) {
			return resps;
		}
		final CountDownLatch latch = new CountDownLatch(2);
		projectList.forEach(e -> {
			new Thread(() -> {
				String contractId = e.getRawRelationContract();
				// 直接关联合同
				if (StrUtil.isNotEmpty(contractId)) {
					Map<String, List<Object>> condition = new HashMap<>(1);
					condition.put("id", Arrays.asList(contractId, "="));
					setOfficeContractInfoResp(resps, condition);
				}
				latch.countDown();
			}).start();

			new Thread(() -> {
				String customerId = e.getRawManagerCustomer();
				// 关联客户再关联合同
				if (StrUtil.isNotEmpty(customerId)) {
					Map<String, List<Object>> condition = new HashMap<>(1);
					condition.put("field_3", Arrays.asList(customerId, "="));
					setOfficeContractInfoResp(resps, condition);
				}
				latch.countDown();
			}).start();
		});
		try {
			latch.await();
		} catch (InterruptedException e) {
			log.error("listContractInfo thread exception", e);
		}
		return resps.stream()
				.collect(Collectors.collectingAndThen(
						Collectors.toCollection(
								() -> new TreeSet<>(Comparator.comparing(OfficeContractInfoResp::getContractCode))),
						ArrayList::new));
	}

	private void setOfficeContractInfoResp(List<OfficeContractInfoResp> resps, Map<String, List<Object>> condition) {
		List<JSONObject> jsonObjects = contractService.listContractInfo(condition);
		jsonObjects.forEach(contract -> {
			OfficeContractInfoResp build = OfficeContractInfoResp.builder().contractCode(contract.getStr("number"))
					.contractTitle(contract.getStr("title")).contractStatus(contract.getStr("field_41"))
					.contractStartTime(contract.getStr("field_12")).contractEndTime(contract.getStr("field_13"))
					.firstPartyName(contract.getStr("tyfJiaFangMingCheng")).secondPartyName(contract.getStr("field_5"))
					.isLotting(contract.getStr("raw_field_18")).signChannel(contract.getStr("raw_field_27"))
					.point(contract.getStr("field_7")).contractConfigType(contract.getStr("field_14")).build();
			resps.add(build);
		});
	}
}
